/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package control;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import model.Gebruiker;
import model.Page;
import model.Student;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;

/**
 *
 * @author woutermkievit
 */
public class SchoolContentManager extends HttpServlet {

    public static SessionFactory sessionFactory;

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
    public Student user = new Student();
    HttpSession sessie;
    Session session;
    String EMPTYPAGE = "/empty.jsp";
    String titlePage = "/titleBar.jsp";
    String userStuffPage = "/empty.jsp";
    String contentPage = "/empty.jsp";
    List<String> menu = new ArrayList();
    String location = "Home";
    MenuHandler menuHandler;
    Page page = null;

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        try {
            sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();

            System.out.println(sessionFactory.isClosed());
        } catch (Throwable e) {
            System.out.println("factory fail: " + e);
        }

        sessie = request.getSession(); //Get data from the session
        if (sessie.getAttribute("titlePage") == null) {
            sessie.setAttribute("titlePage", "/titleBar.jsp");
        }
        if (sessie.getAttribute("userStuffPage") == null) {
            sessie.setAttribute("userStuffPage", "/login.jsp");
        }
        if (sessie.getAttribute("contentPage") == null) {
            sessie.setAttribute("contentPage", "/homepage.jsp");
        }
        loadPage(request, response);
    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        try {
            sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
        } catch (Throwable e) {
            System.out.println(e);
        }
        sessie = request.getSession(); //Get data from the session
        loadPage(request, response);

    }

    public void loadPage(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

        titlePage = (String) sessie.getAttribute("titlePage");
        userStuffPage = (String) sessie.getAttribute("userStuffPage");
        contentPage = (String) sessie.getAttribute("contentPage");
        menuHandler = new MenuHandler(sessie);

        menuHandler.menuAdd("viewCourse&name=algebra%202", "Demo");
        location = (String) sessie.getAttribute("location");
        session = sessionFactory.getCurrentSession();
                Transaction transaction = session.beginTransaction();
                page = (Page) session.get(Page.class, "0");
                request.setAttribute("title", page.getTitle());
                request.setAttribute("subtitle", page.getSubtitle());
                request.setAttribute("message", page.getMessage().replaceAll("\n", "<br>"));
                transaction.commit();

        if (sessie.getAttribute("user") == null) {
        } else {
            if (sessie.getAttribute("contentPage").equals(EMPTYPAGE)) {
                contentPage = "homepage.jsp";
                
            }

            userStuffPage = EMPTYPAGE;
        }
        String uname = "";
        if (sessie.getAttribute("cuser") != null) {
            uname = "Welcome, " + ((Gebruiker) sessie.getAttribute("cuser")).getName();
        } else {
            uname = "";
        }
        request.setAttribute("fout", sessie.getAttribute("fout"));
        request.setAttribute("registreerfout", sessie.getAttribute("registreerfout"));
        request.setAttribute("titlePage", titlePage);
        request.setAttribute("userStuff", userStuffPage);
        request.setAttribute("content", contentPage);
        request.setAttribute("unaam", uname);
        request.setAttribute("menu", menuHandler.get());
        request.setAttribute("menu2", menuHandler.getList());
        /*sessie.setAttribute("titlePage", titlePage);
        sessie.setAttribute("userStuffPage", userStuffPage);
        sessie.setAttribute("contentPage", contentPage);
        sessie.setAttribute("menuPage",  menuPage);
        sessie.setAttribute("location", location);*/
        String address = "/main.jsp";
        RequestDispatcher dispatcher = request.getRequestDispatcher(address);
        dispatcher.forward(request, response);
    }
}
